﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace PPE_gestion_commercial
{
    public partial class Form1 : Form
    {
        SqlConnection connex;
        SqlCommand cmd;
        SqlDataReader dr;
        SqlDataReader dr2;
        
        public static string selection;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int j = 0;

            dataGridView1.ColumnCount = 3;
            dataGridView2.ColumnCount = 4;
            dataGridViewCommande.ColumnCount = 5;

            dataGridViewCommande.Columns[0].Name = "Nom du produit";
            dataGridViewCommande.Columns[1].Name = "Prix unitaire";
            dataGridViewCommande.Columns[2].Name = "quantité";
            dataGridViewCommande.Columns[3].Name = "prix total";
            dataGridViewCommande.Columns[4].Name = "id produit";
            dataGridViewCommande.Columns[4].Visible = false;

            dataGridView1.Columns[0].Name = "Nom du Client";
            dataGridView1.Columns[1].Name = "Prenom du Client";
            dataGridView1.Columns[2].Name = "id produit";
            dataGridView1.Columns[2].Visible = false;
            
            dataGridView2.Columns[0].Name = "Nom du Produit";
            dataGridView2.Columns[1].Name = "Prix";
            dataGridView2.Columns[2].Name = "Quantité";
            dataGridView2.Columns[3].Name = "id produit";
            dataGridView2.Columns[3].Visible = false;

            string strconn = "data source=FRACO-PC;database=gestion;trusted_connection=yes";
            connex = new SqlConnection();
            connex.ConnectionString = strconn;
            connex.Open();

            string sql = "Select NomClient,PrenomClient,IdClient from client";
            cmd = new SqlCommand(sql, connex);
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                dataGridView1.RowCount = j + 1;                
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    dataGridView1.Rows[j].Cells[i].Value = dr[i].ToString();
                }
                j++;
            }

            dr.Close();

            j = 0;
            
            string sql2 = "Select NomProduit,PrixP,QuantiteP,IdProduit from produit";
            cmd = new SqlCommand(sql2, connex);
            dr2 = cmd.ExecuteReader();

            while (dr2.Read())
            {
                dataGridView2.RowCount = j + 1;                
                for (int i = 0; i < dr2.FieldCount ; i++)
                {
                    dataGridView2.Rows[j].Cells[i].Value = dr2[i].ToString();
                }
                j++;
            }

            dr2.Close();

            dataGridView1.AutoResizeColumns();
            dataGridView2.AutoResizeColumns();
        }

        private void commandeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form1 f1 = new Form1();
            f1.Show();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int colonne = e.ColumnIndex;
            int ligne = e.RowIndex;

            selection = this.dataGridView1.Rows[ligne].Cells[colonne].Value.ToString();
            Form3 f3 = new Form3(selection,colonne);
            f3.Show();
        }

        private int CalculerNouveauPrixTotal()
        {
            Int32 Total = 0;
            foreach (DataGridViewRow LigneCommande in dataGridViewCommande.Rows)
            {
                Total = Total + Convert.ToInt16(LigneCommande.Cells[2].Value) * Convert.ToInt16(LigneCommande.Cells[1].Value);
            }
            TextBoxTotalPrixCommande.Text = Convert.ToString(Total);
            return Total;
        }

        private void buttonCommande_Click(object sender, EventArgs e)
        {
            Int16 unSeulClientALaFois = 0;
            foreach (DataGridViewCell cellules in dataGridView1.SelectedCells) // on selectionne toutes les lignes selectionées partilement
            {
                if (unSeulClientALaFois < 1)
                {
                    dataGridView1.Rows[cellules.RowIndex].Selected = true;
                    unSeulClientALaFois++;
                }
            }
            foreach (DataGridViewCell cellules in dataGridView2.SelectedCells) // on selectionne toutes les lignes selectionées partilement
            {
                dataGridView2.Rows[cellules.RowIndex].Selected = true;
            }
            foreach( DataGridViewRow dg in dataGridView2.SelectedRows)
            {
                //verification si la ligne est deja presente dans les lignes de la commande
                bool dejaCommande = false;
                foreach (DataGridViewRow LigneCommande in dataGridViewCommande.Rows)
                {
                    if (LigneCommande.Cells[4].Value == dg.Cells[3].Value)
                    {
                        dejaCommande = true;
                        LigneCommande.Cells[2].Value = Convert.ToInt16(LigneCommande.Cells[2].Value) + Convert.ToInt16( numericUpDownCommande.Value);
                        LigneCommande.Cells[3].Value = Convert.ToInt16(LigneCommande.Cells[2].Value) * Convert.ToInt16(LigneCommande.Cells[1].Value);
                    }
                }
                if (dejaCommande == false)
                {
                    //ajout de la ligne sinon
                    dataGridViewCommande.RowCount++;
                    dataGridViewCommande.Rows[dataGridViewCommande.RowCount - 1].Cells[0].Value = dg.Cells[0].Value;
                    dataGridViewCommande.Rows[dataGridViewCommande.RowCount - 1].Cells[1].Value = dg.Cells[1].Value;
                    dataGridViewCommande.Rows[dataGridViewCommande.RowCount - 1].Cells[2].Value = numericUpDownCommande.Value;
                    dataGridViewCommande.Rows[dataGridViewCommande.RowCount - 1].Cells[3].Value = Convert.ToInt16(dg.Cells[1].Value) * Convert.ToInt16(numericUpDownCommande.Value);
                    dataGridViewCommande.Rows[dataGridViewCommande.RowCount - 1].Cells[4].Value = dg.Cells[3].Value;
                }
                CalculerNouveauPrixTotal();
            }
        }

        private void supprimerLigneCommande_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow drg in dataGridViewCommande.SelectedRows)
            {
                dataGridViewCommande.Rows.Remove(drg);
            }
            CalculerNouveauPrixTotal();
        }

    }
}
